package com.example.websocket;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "demo")
public class WebController {

    @Autowired
    SimpMessagingTemplate simpMessagingTemplate;

    @MessageMapping(value = "/sendPoint")
    @SendTo(value = "/topic/getResponse")
    public Message point(Form form){


        for (int i=0;i<10;i++){
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Message message = new Message();
            message.setName(""+i);
            message.setMsg(form.getName());
            simpMessagingTemplate.convertAndSend("/topic/getResponse",message);
        }

        Message message = new Message();
        message.setName("");
        message.setMsg(form.getName());
        return message;
    }




    public static class Form{

        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    public static class Message{

        private String name;

        private String msg;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }
    }


}
